using Firestarter.DB.Schema.BO;
using FireStarterModeller.BO;
using FireStarterModeller.Logic;
using FireStarterModeller.Test;
using FireStarterModeller.Test.BO;
using NUnit.Framework;
 
using Rhino.Mocks;

namespace Firestarter.DB_DomainModel.Logic.Test.DBChangesImporterTests
{
    [TestFixture]
    public class TestDBChangesImporter_ChangeTypeIsNone : TestBase
    {
        private static DBChangesImporter CreateDBChangesImporter()
        {
            IDBTypeToDMPropertyTypeMapper propertyTypeMapper = MockRepository.GenerateStub<IDBTypeToDMPropertyTypeMapper>();
            DBToDMImporterFactory factory = new DBToDMImporterFactory(propertyTypeMapper, new RelationshipNameGenerator());
            return new DBChangesImporter(factory);
        }


        // Story: Should test when None and Approved/UnApproved it should not change the Approved/Unapproved Value

        [Test]
        public void Test_ImportDB_NewDBTables_ShouldNotAddWhenChangeTypeIsNone()
        {
            DBDatabase database = new DBDatabase();
            database.Tables.Add(new DBTable(TestUtilsShared.GetRandomString()).ApproveForAdd());
            database.Tables.Add(new DBTable(TestUtilsShared.GetRandomString()).ApproveForNone());
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            DBChangesImporter importer = CreateDBChangesImporter();
            //---------------Assert Precondition----------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(0));
            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(1));
        }

        [Test]
        public void Test_ImportDB_NewDBProps_ShouldNotAddWhenChangeTypeIsNone()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString()).ApproveForAdd();
            database.Tables.Add(table);
            DBTableBuilder builder = new DBTableBuilder(table);
            builder.AddPrimaryKeyColumn().ApproveForNone();
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = CreateDBChangesImporter();
            //---------------Assert Precondition----------------
            Assert.That(solution.AllClasses.Count, Is.EqualTo(0));
            Assert.That(database.Tables.Count, Is.Not.EqualTo(0));
            Assert.That(database.Tables[0].Columns.Count, Is.Not.EqualTo(0));
            Assert.That(solution.DefaultAssembly, Is.Not.Null);
            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses[0].Properties.Count, Is.EqualTo(0));
        }

        [Test]
        public void Test_ImportDB_NewDBPrimaryKey_ShouldNotAddWhenChangeTypeIsNone()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString()).ApproveForAdd();
            database.Tables.Add(table);
            DBTableBuilder builder = new DBTableBuilder(table);
            builder.AddPrimaryKeyColumn().ApproveForAdd();
            table.PrimaryKeyDB.ApproveForNone();
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = CreateDBChangesImporter();
            //---------------Assert Precondition----------------
            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses[0].ObjectIdentity, Is.Null);
        }

        [Test]
        public void Test_ImportDB_NewDBKey_ShouldNotAddWhenChangeTypeIsNone()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            DBTable table = new DBTable(TestUtilsShared.GetRandomString()).ApproveForAdd();
            database.Tables.Add(table);
            DBTableBuilder builder = new DBTableBuilder(table);
            builder.AddUniqueKeyColumn("ExistingAKColumn").ApproveForAdd();
            table.Keys[0].ApproveForNone();
            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = CreateDBChangesImporter();
            //---------------Assert Precondition----------------
            Assert.AreEqual(1, table.Columns.Count);
            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            Assert.That(solution.AllClasses[0].UniqueConstraints.Count, Is.EqualTo(0));
        }

        [Test]
        public void Test_ImportDB_NewDBForeignKey_ShouldNotAddWhenChangeTypeIsNone()
        {
            //---------------Set up test pack-------------------
            DBDatabase database = new DBDatabase();
            string tableName1 = TestUtilsShared.GetRandomString();
            DBTable table1 = new DBTable(tableName1).ApproveForAdd();
            database.Tables.Add(table1);
            string tableName2 = TestUtilsShared.GetRandomString();
            DBTable table2 = new DBTable(tableName2).ApproveForAdd();
            database.Tables.Add(table2);

            DBTableBuilder builder1 = new DBTableBuilder(table1);
            builder1.AddPrimaryKeyColumn().ApproveForAdd();
            DBTableBuilder builder2 = new DBTableBuilder(table2);
            DBForeignKey foreignKey = builder2.AddForeignKey(TestUtilsShared.GetRandomString(), table1).ApproveForNone();
            foreignKey.ForeignKeyColumns[0].ApproveForAdd().ParentColumn.ApproveForAdd();

            DMSolution solution = TestUtilsDMSolution.CreateUnsavedValidDMSolution();
            
            DBChangesImporter importer = CreateDBChangesImporter();
            //---------------Assert Precondition----------------

            //---------------Execute Test ----------------------
            importer.ImportDBChanges(database, solution);
            //---------------Test Result -----------------------
            DMClass dmClass2 = solution.AllClasses.Find(class1 => class1.MappedTableName == tableName2);
            Assert.That(dmClass2.OwnerRelationships.Count, Is.EqualTo(0));
        }
    }
}